/**
 * 
 */
package com.changil.nowonchangilchurch.protocol;

import org.apache.http.Header;
import org.json.JSONObject;

import android.content.Context;

import com.changil.nowonchangilchurch.protocol.dto.ArticleList;
import com.changil.nowonchangilchurch.protocol.http.DataLoadListener;
import com.changil.nowonchangilchurch.protocol.http.request.ArticleListRequest;
import com.changil.nowonchangilchurch.protocol.http.response.ArticleListResponse;
import com.loopj.android.http.JsonHttpResponseHandler;

/**
 * @author zeropol2
 * 
 */
public class ApiManager {
	private static ApiManager sInstance = null;
	private Context mContext = null;

	private ApiManager(Context mContext) {
		this.mContext = mContext;
	}

	public static ApiManager getsInstance() {
		return sInstance;
	}

	public static ApiManager getsInstance(Context context) {
		if (sInstance == null) {
			synchronized (ApiManager.class) { // 성능을 고려하여 if 문 안쪽에 synchronized
												// 구문을 배치
				if (sInstance == null) { // 최상위 if 구문이 asynch 문 이기 때문에 한번 더 검사해야
											// 한다.
					sInstance = new ApiManager(context);
				}
			}
		}
		return sInstance;
	}
	
	public void getArticleList(final DataLoadListener<ArticleList> listener,int boardId, int fromId, int count, boolean isDownSide) {
		ArticleListRequest request = new ArticleListRequest(boardId, fromId, count, isDownSide);
		request.request(new JsonHttpResponseHandler("UTF-8") {

			@Override
			public void onSuccess(JSONObject response) {
				ArticleListResponse httpResponse = new ArticleListResponse();
				listener.onSuccess((ArticleList)httpResponse.response(response));
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				listener.onFail();
			}
		});
	}

}
